package com.fr.fileTest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
*作者：furong
*日期：2016年11月14日
*时间：下午6:47:06
*/
public class FileTest {
    public static void main(String[] args) throws URISyntaxException, IOException {
        // URL url = new URL(
        // "http://ho-test.oss.aliyuncs.com/company/20161114/18/b8d5a162-6ef3-4982-bcbb-f3d4c99536da.jpg?Expires=1794480369&OSSAccessKeyId=4aLE8QGSsmACxcyU&Signature=PweWDTpoSdNn%2B%2FKAIJm%2BGTRchzQ%3D");
        // URI uri = url.toURI();
        // System.out.println(uri.getScheme());
        //
        // File file = new File(uri);
        // System.out.println(file.getPath());
        URL url = new URL(
                "http://ho-test.oss.aliyuncs.com/authorization/20161118/17/af5e285c-f3f2-471d-a47e-3851a7bef0b7.2.pdf?Expires=1794820320&OSSAccessKeyId=4aLE8QGSsmACxcyU&Signature=ZD0V7QpLPvI1qhqjqv0YL8NSczE%3D");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(3000);
        conn.setReadTimeout(60000);

        conn.connect();
        InputStream in = conn.getInputStream();
        List<Byte> list = new ArrayList<>();
        byte[] b = new byte[4096];
        // while (is.read(b) > 0) {
        // for (byte bb : b) {
        // list.add(bb);
        // }
        // }

        // File f = new File("F:\\2.pdf");
        // FileInputStream fi = new FileInputStream(f);
        File file = new File("F:\\test.pdf");
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream fo = new FileOutputStream(file);
        int lenth = -1;
        while ((lenth = in.read(b)) > 0) {
            for (int i = 0; i < lenth; i++) {
                list.add(b[i]);
            }
        }
        byte[] bytes = new byte[list.size()];
        for (int i = 0; i < list.size(); i++) {
            bytes[i] = list.get(i);
        }
        fo.write(bytes);
        fo.flush();
        fo.close();
    }
}
